package com.aaa.account.service.impl;

import com.aaa.account.entity.TbAccAccount;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.account.entity.Manager;
import com.aaa.account.dao.ManagerMapper;
import com.aaa.account.service.ManagerService;

@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    private Logger logger = LoggerFactory.getLogger(ManagerServiceImpl.class);


    @Resource
    ManagerMapper managerMapper;

    public List<Manager> listByUsername(String username) {
        QueryWrapper<Manager> travelsQueryWrapper = new QueryWrapper<>();
        travelsQueryWrapper.like("username", username);
        return this.list(travelsQueryWrapper);
    }

    public List<Manager> listDesc() {
        QueryWrapper<Manager> tbPrdInfoQueryWrapper = new QueryWrapper<>();
        tbPrdInfoQueryWrapper.orderByDesc("mid");
        return this.list(tbPrdInfoQueryWrapper);
    }

    public boolean saveManager(Manager manager) {
        //把密码转换为密钥
        String password = manager.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        manager.setPassword(encode);

        String username = manager.getUsername();
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        List<Manager> managers = managerMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(managers)){
            logger.warn("{}-用户已存在，请选择其它用户名!",username);
            return false;
        }
        int insert = managerMapper.insert(manager);
        logger.info("{}注册成功！",username);
        return insert > 0 ? true : false;
    }
}


